package com.mlh.stack;

import java.util.Stack;

/**
 * @author 缪林辉
 * @date 2024/3/21 10:13
 * @DESCRIPTION
 */
//思路  一个栈专门进A栈  一个栈专门出B栈
//B栈空了  就把A栈元素都都弹到B栈
//这样就能模拟队列
class MyQueue {
    Stack<Integer> stack1;
    Stack<Integer> stack2;

    public MyQueue() {
        this.stack1 = new Stack<Integer>();
        this.stack2 = new Stack<Integer>();
    }

    public void push(int x) {
       stack1.push(x);
    }

    public int pop() {
        //利用peek来触发 A栈元素弹到B栈  减少代码冗余
        if(peek()!=-1){
            return stack2.pop();
        }
        return -1;
    }

    public int peek() {
        if(empty()){
            return -1;
        }
        //B栈为空 触发 A栈元素弹到B栈
        if (stack2.isEmpty()) {
            while (!stack1.isEmpty()){
                stack2.push(stack1.pop());
            }
        }
        return stack2.peek();
    }

    public boolean empty() {
        return stack1.empty()&& stack2.empty();
    }
}